package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.db.domain.system.MpClientOrder;
import com.ruoyi.db.domain.system.MpClientSubOrder;
import com.ruoyi.db.domain.system.PcProCombo;
import com.ruoyi.db.domain.system.PcProduct;
import com.ruoyi.db.domain.web.MpClientAddress;
import com.ruoyi.db.mapper.system.MpClientOrderMapper;
import com.ruoyi.db.mapper.system.MpClientSubOrderMapper;
import com.ruoyi.db.mapper.system.PcProComboMapper;
import com.ruoyi.db.mapper.system.PcProductMapper;
import com.ruoyi.db.mapper.web.MpClientAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IMpClientOrderService;

import javax.transaction.Transactional;

/**
 * 【商城用户】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-23
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpClientOrderServiceImpl implements IMpClientOrderService 
{
    @Autowired
    private MpClientOrderMapper mpClientOrderMapper;
    @Autowired
    private MpClientSubOrderMapper mpClientSubOrderMapper;
    @Autowired
    private PcProductMapper pcProductMapper;
    @Autowired
    private PcProComboMapper pcProComboMapper;
    @Autowired
    private MpClientAddressMapper mpClientAddressMapper;

    /**
     * 查询【商城用户】
     * 
     * @param orderId 【商城用户】主键
     * @return 【商城用户】
     */
    @Override
    public MpClientOrder selectMpClientOrderByOrderId(String orderId)
    {
        MpClientOrder order = mpClientOrderMapper.selectMpClientOrderByOrderId(orderId);
            MpClientSubOrder subOrder = new MpClientSubOrder();
            subOrder.setOrderId(order.getOrderId());
            List<MpClientSubOrder> subs = mpClientSubOrderMapper.selectMpClientSubOrderList(subOrder);
            for (MpClientSubOrder subOrder1: subs) {
                if(subOrder1.getProId()!=null){
                    PcProduct pcProduct = pcProductMapper.selectPcProductByProId(subOrder1.getProId());
                    subOrder1.setProName(pcProduct.getProName());
                    subOrder1.setSellType(pcProduct.getSellType().intValue());
                    subOrder1.setMasterImg(pcProduct.getMasterImg());
                    subOrder1.setRmbPrice(pcProduct.getRmbPrice());
                    subOrder1.setGoldBeanPrice(pcProduct.getGoldBeanPrice());
                    subOrder1.setSilverBeanPrice(pcProduct.getSilverBeanPrice());
                }else if(subOrder1.getProComboId()!=null){
                    PcProCombo pcProCombo = pcProComboMapper.selectPcProComboByProComboId(subOrder1.getProComboId());
                    subOrder1.setProName(pcProCombo.getProComboName());
                    subOrder1.setSellType(1);
                    subOrder1.setMasterImg(pcProCombo.getMasterImg());
                    subOrder1.setRmbPrice(pcProCombo.getRmbPrice());
                }
            }
            order.setSubOrders(subs);
            return order;
    }

    /**
     * 查询【商城用户】列表
     * 
     * @param mpClientOrder 【商城用户】
     * @return 【商城用户】
     */
    @Override
    public List<MpClientOrder> selectMpClientOrderList(MpClientOrder mpClientOrder)
    {
       List<MpClientOrder> list = mpClientOrderMapper.selectMpClientOrderList(mpClientOrder);
        for (MpClientOrder o:list) {
            MpClientAddress address  = mpClientAddressMapper.selectMpClientAddressByAddressId(o.getAddressId());
            if(address!=null){
               o.setContactName(address.getContactName());
                o.setContactMobile(address.getContactPhone());
                o.setAddress((address.getProvinceName()==null?"":address.getProvinceName())+(address.getCityName()==null?"":
                        address.getCityName())+(address.getCountyName()==null?"":address.getCountyName())+(address.getAddress()==null?
                        "":address.getAddress()));
            }
            MpClientSubOrder subOrder = new MpClientSubOrder();
            subOrder.setOrderId(o.getOrderId());
            List<MpClientSubOrder> subs = mpClientSubOrderMapper.selectMpClientSubOrderList(subOrder);
            for (MpClientSubOrder subOrder1: subs) {
                if(subOrder1.getProId()!=null){
                    PcProduct pcProduct = pcProductMapper.selectPcProductByProId(subOrder1.getProId());
                    o.setProName(pcProduct.getProName());
                }else if(subOrder1.getProComboId()!=null){
                    PcProCombo pcProCombo = pcProComboMapper.selectPcProComboByProComboId(subOrder1.getProComboId());
                    o.setCombName(pcProCombo.getProComboName());
                }
            }
        }
       return list;
    }

    /**
     * 新增【商城用户】
     * 
     * @param mpClientOrder 【商城用户】
     * @return 结果
     */
    @Override
    public int insertMpClientOrder(MpClientOrder mpClientOrder)
    {
        return mpClientOrderMapper.insertMpClientOrder(mpClientOrder);
    }

    /**
     * 修改【商城用户】
     * 
     * @param mpClientOrder 【商城用户】
     * @return 结果
     */
    @Override
    public int updateMpClientOrder(MpClientOrder mpClientOrder)
    {
        return mpClientOrderMapper.updateMpClientOrder(mpClientOrder);
    }

    /**
     * 批量删除【商城用户】
     * 
     * @param orderIds 需要删除的【商城用户】主键
     * @return 结果
     */
    @Override
    public int deleteMpClientOrderByOrderIds(String[] orderIds)
    {
        return mpClientOrderMapper.deleteMpClientOrderByOrderIds(orderIds);
    }

    /**
     * 删除【商城用户】信息
     * 
     * @param orderId 【商城用户】主键
     * @return 结果
     */
    @Override
    public int deleteMpClientOrderByOrderId(String orderId)
    {
        return mpClientOrderMapper.deleteMpClientOrderByOrderId(orderId);
    }

    @Override
    public void sendOrderByOrderId(MpClientOrder mpClientOrder) {
        MpClientOrder order = mpClientOrderMapper.selectMpClientOrderByOrderId(mpClientOrder.getOrderId());
        order.setExpressId(mpClientOrder.getExpressId());
        order.setLogisticsNumber(mpClientOrder.getLogisticsNumber());
        order.setOrderStatus(301l);
        mpClientOrderMapper.updateMpClientOrder(order);

    }
}
